package summer.hot100.v1;

import java.util.LinkedHashMap;
import java.util.Map;

public class Solution146 {

}

class LRUCache {
    int size;
    Map<Integer, Integer> map;

    public LRUCache(int capacity) {
        this.size = capacity;
        map = new LinkedHashMap<>(capacity);
    }

    public int get(int key) {
        return makeRecent(key);
    }

    public void put(int key, int value) {
        if (map.containsKey(key)) {
            makeRecent(key);
            map.put(key, value);
        } else {
            if (map.size() >= size) {
                int del = map.keySet().iterator().next();
                map.remove(del);
            }
            map.put(key, value);
        }
    }

    public int makeRecent(int key) {
        if (!map.containsKey(key)) return -1;
        else {
            Integer val = map.get(key);
            map.remove(key);
            map.put(key, val);
            return val;
        }
    }
}